# 携带本地用户信息启动selenium（携带插件 携带cookie）
# from selenium import webdriver
#
# # 启动前 关掉其他谷歌浏览器实例
# options = webdriver.ChromeOptions()
# options.add_argument(r"--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")
# driver = webdriver.Chrome(options=options)
#
# # driver = webdriver.Chrome()
# driver.get("https://kns.cnki.net/kns8s/defaultresult/index?korder=&kw=")
# input()


# 避免被检测使用自动化技术
# from selenium import webdriver
#
# options = webdriver.ChromeOptions()
# # 此步骤很重要，设置为开发者模式，防止被各大网站识别出来使用了Selenium
# options.add_experimental_option('excludeSwitches', ['enable-automation'])
# # 就是这一行告诉chrome去掉了webdriver痕迹，令navigator.webdriver=false，极其关键
# options.add_argument("--disable-blink-features=AutomationControlled")
# # 添加浏览器用户信息 携带cookie访问免登录
# options.add_argument(r"--user-data-dir=C:\Users\Administrator\AppData\Local\Google\Chrome\User Data")
# driver = webdriver.Chrome(options=options)
# driver.get("https://www.xinpianchang.com/discover/article-0-0-all-all-0-0-pick-pp100")
# input()


# selenium三种等待方式：强制等待（time.sleep） 隐式等待（driver.implicitly_wait） 显式等待（WebDriverWait）
# from selenium import webdriver
# import time
#
# driver = webdriver.Chrome()
# driver.get("https://www.pexels.com/zh-cn/")
# # driver.implicitly_wait(180)  # 隐式等待10秒 timeout
# # 显示等待 每隔一个较小的时间间隔查询元素 找到了程序继续进行 没找到就继续等待
# # 自己实现：
# start_time = time.time()
# def find_element(driver, xpath):
#     for i in range(100):
#         try:
#             element = driver.find_elements("xpath", xpath)
#             return element
#         except:
#             time.sleep(0.5)
#             print("继续等待")
#     return None
# div_list = find_element(driver, "//div[@class='BreakpointGrid_item__RSMyf']")
# print(len(div_list))
# print("耗时：", time.time() - start_time)
# input()

# 官方自带的显式等待用法
# from selenium import webdriver
# # expected_conditions 类，负责条件触发
# from selenium.webdriver.support import expected_conditions as EC
# # WebDriverWait 库，负责循环等待
# from selenium.webdriver.support.ui import WebDriverWait
# # 查找方式的库
# from selenium.webdriver.common.by import By
#
# driver = webdriver.Chrome()
# # 用户名的输入框
# keyword = WebDriverWait(driver,30).until(
#     EC.presence_of_element_located((By.XPATH, '//div[@class="subWp Ldn"]/div[1]//input'))
# )


# 提高selenium的运行效率：无头浏览器 禁用图片加载
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
chrome_options.add_experimental_option("prefs", prefs)
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.bilibili.com/")
print(driver.page_source)
input()




